﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BioImg;
using GMap.NET;
using GMap.NET.Projections;
using GMap.NET.WindowsForms;
using GMap.NET.WindowsForms.Markers;

namespace GMapClient
{
    public partial class Form1 : Form
    {

        private GMapOverlay polygons = new GMapOverlay("polygon"); //放置polygon的图层
        private GMapMarkerPolygon drawingCircle = null; //正在画的circle

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.gMapControl1.CacheLocation = Path.Combine(System.Windows.Forms.Application.StartupPath,"Gmap");//指定地图缓存存放路径
            gMapControl1.Manager.Mode = GMap.NET.AccessMode.ServerOnly;// ServerOnly;// ServerAndCache;
            gMapControl1.MapProvider = BioImgProvider.Instance;
            gMapControl1.ShowCenter = false; //不显示中心十字点
            gMapControl1.MinZoom = 0;
            gMapControl1.MaxZoom = 10;// (int)BioImgProvider.Instance.MaxZoom;// 10;
            gMapControl1.Zoom = 6;
            gMapControl1.DragButton = System.Windows.Forms.MouseButtons.Left;
            PointLatLng lPoint = MercatorProjection.Instance.FromPixelToLatLng(0, 0, 6);
            gMapControl1.Position = lPoint;

            gMapControl1.MouseWheel += GMapControl1_MouseWheel;
            gMapControl1.Paint += GMapControl1_Paint;
            gMapControl1.MouseDown += MapControl_MouseDown;
            gMapControl1.MouseMove += MapControl_MouseMove;
            gMapControl1.MouseUp += MapControl_MouseUp;
            drawingCircle = new GMapMarkerPolygon(lPoint);
            polygons.Markers.Add(drawingCircle);
            gMapControl1.Overlays.Add(polygons);


            string str = "100X Oil x 0.35";
              //str = "100X Oil ";

           
            


        }
       
        private void initDraw()
        {
            PointLatLng lPoint1 = MercatorProjection.Instance.FromPixelToLatLng(97756, 32340, 10);
            PointLatLng lPoint2 = MercatorProjection.Instance.FromPixelToLatLng(98014, 32520, 10);
            PointLatLng lPoint3 = MercatorProjection.Instance.FromPixelToLatLng(97756, 32520, 10);

            List<PointLatLng> points = new List<PointLatLng>();
            points.Add(lPoint1);
            points.Add(lPoint2);
            points.Add(lPoint3);

            GMapMarkerPolygon drawingMarkrt = new GMapMarkerPolygon(lPoint1);
            drawingMarkrt.IsMeter = false;
            drawingMarkrt.isDrawed = true;
            drawingMarkrt.m_drawType = AreaStruct.AreaDrawType.DT_POLYGON;
            polygons.Markers.Add(drawingMarkrt);
            drawingMarkrt.drawingPoints = points;
            gMapControl1.UpdateMarkerLocalPosition(drawingMarkrt);
            gMapControl1.Refresh();
        }



        void DrawCircleRadius()
        {
            PointLatLng lPoint1 = MercatorProjection.Instance.FromPixelToLatLng(97756, 32340, 10);
            PointLatLng lPoint2 = MercatorProjection.Instance.FromPixelToLatLng(98014, 32520, 10);
            drawingCircle = new GMapMarkerPolygon(lPoint1);
            drawingCircle.isDrawed = true;
            drawingCircle.m_drawType = AreaStruct.AreaDrawType.DT_CIRCLE;
            drawingCircle.IsMeter = false;
            drawingCircle.addPoint(lPoint2);
            polygons.Markers.Add(drawingCircle);
            gMapControl1.UpdateMarkerLocalPosition(drawingCircle);
            gMapControl1.Refresh();
        }

        #region GMapControl1_Paint
        private void GMapControl1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            int z = (int)(10 - gMapControl1.Zoom);
            long x = gMapControl1.PositionPixel.X << z;
            long y = gMapControl1.PositionPixel.Y << z;

            double rate = (double)1 / ((long)Math.Pow(2, z));
            string str = string.Format($"{x},{y}");
            str += "x:" + gMapControl1.PositionPixel.X + "y:" + gMapControl1.PositionPixel.Y + "z:" + gMapControl1.Zoom.ToString("G0") + "rate:" + rate.ToString();
            g.DrawString(str, new Font("宋体", 20), new SolidBrush(Color.Red),
                              new Point(0, 0));
        }
        #endregion




        private void 当前位置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int z = (int)(10 - gMapControl1.Zoom);
            //long x = gMapControl1.PositionPixel.X << z;
            //long y = gMapControl1.PositionPixel.Y << z;
            long x = gMapControl1.PositionPixel.X * (long)Math.Pow(2, z);
            long y = gMapControl1.PositionPixel.Y * (long)Math.Pow(2, z);
            MessageBox.Show(string.Format($"{x},{y}"));

        }







        #region MapControl事件


        private void GMapControl1_MouseWheel(object sender, MouseEventArgs e)
        {
        }
        private void MapControl_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left && m_drawType != AreaStruct.AreaDrawType.DT_NONE)
            {
                switch (m_drawType)
                {
                    case AreaStruct.AreaDrawType.DT_CIRCLE:
                        polygons_MouseDown(sender, e);
                        break;
                    case AreaStruct.AreaDrawType.DT_POLYGON:
                        polygons_MouseDown(sender, e);
                        break;
                    case AreaStruct.AreaDrawType.DT_RECTANGLE:
                        #region MyRegion
                        polygons_MouseDown(sender, e);
                        #endregion
                        break;
                }
            }
            else if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                drawingCircle.drawingPoints.Clear();
            }
        }

        private void MapControl_MouseUp(object sender, MouseEventArgs e)
        {
            if (m_drawType != AreaStruct.AreaDrawType.DT_NONE)
            {
                //switch (m_drawType)
                //{
                //    case AreaStruct.AreaDrawType.DT_CIRCLE:
                //        circles_MouseUp(sender, e);
                //        break;
                //    case AreaStruct.AreaDrawType.DT_POLYGON:
                //        polygons_MouseUp(sender, e);
                //        break;
                //}
            }
        }
        private void MapControl_MouseMove(object sender, MouseEventArgs e)
        {
            if (m_drawType != AreaStruct.AreaDrawType.DT_NONE)
            {
                switch (m_drawType)
                {
                    case AreaStruct.AreaDrawType.DT_CIRCLE:
                        polygons_MouseMove(sender, e);
                        break;
                    case AreaStruct.AreaDrawType.DT_POLYGON:
                        polygons_MouseMove(sender, e);
                        break;
                    case AreaStruct.AreaDrawType.DT_RECTANGLE:
                        polygons_MouseMove(sender, e);
                        break;
                }
            }
        }
        void drawingFinish(object sender, MouseEventArgs e)
        {
            if (drawingCircle != null && m_drawType != AreaStruct.AreaDrawType.DT_NONE)
            {
                PointLatLng mouseLastPoint = gMapControl1.FromLocalToLatLng(e.X, e.Y);

                GMapMarkerPolygon drawingCircle2 = new GMapMarkerPolygon(drawingCircle.Position);
                drawingCircle2.m_drawType = drawingCircle.m_drawType;
                drawingCircle2.drawingPoints = drawingCircle.drawingPoints.ToArray().ToList();
                drawingCircle2.addPoint(mouseLastPoint);
                drawingCircle2.isDrawed = true;
                drawingCircle2.IsMeter = false;
                polygons.Markers.Add(drawingCircle2);
                gMapControl1.UpdateMarkerLocalPosition(drawingCircle2);
                gMapControl1.Refresh();
                drawingCircle.drawingPoints.Clear();
            }
        }
        #endregion


        #region circles

        //private void circles_MouseDown(object sender, MouseEventArgs e)
        //{
        //    PointLatLng mouseLastPoint = gMapControl1.FromLocalToLatLng(e.X, e.Y);
        //    if (drawingCircle.drawingPoints.Count == 0)
        //    {
        //        drawingCircle.Position = mouseLastPoint;
        //        drawingCircle.addPoint(mouseLastPoint);
        //        drawingCircle.isDrawed = false;
        //        drawingCircle.IsMeter = false;
        //        polygons.Markers.Add(drawingCircle);
        //    }
        //    else
        //    {
        //        drawingCircle.addPoint(mouseLastPoint);
        //        switch (m_drawType)
        //        {
        //            case AreaStruct.AreaDrawType.DT_CIRCLE:
        //                if (drawingCircle.drawingPoints.Count>=2)
        //                {
        //                    drawingFinish(sender, e);
        //                }
        //                break;
        //            case AreaStruct.AreaDrawType.DT_POLYGON:
        //                //polygons_MouseMove(sender, e);
        //                break;
        //            case AreaStruct.AreaDrawType.DT_RECTANGLE:
        //                if (drawingCircle.drawingPoints.Count >= 2)
        //                {
        //                    drawingFinish(sender, e);
        //                }
        //                break;
        //        }

   
        //        //GMapMarkerPolygon drawingCircle2 = new GMapMarkerPolygon(drawingCircle.Position);
        //        //drawingCircle2.m_drawType = drawingCircle.m_drawType;
        //        //drawingCircle2.drawingPoints = drawingCircle.drawingPoints.ToArray().ToList();
        //        //drawingCircle2.addPoint(mouseLastPoint);
        //        //drawingCircle2.isDrawed = true;
        //        //drawingCircle2.IsMeter = false;
        //        //polygons.Markers.Add(drawingCircle2);
        //        //gMapControl1.UpdateMarkerLocalPosition(drawingCircle2);
        //        //gMapControl1.Refresh();
        //        //drawingCircle.drawingPoints.Clear(); 
        //    }
        //}

        //private void circles_MouseUp(object sender, MouseEventArgs e)
        //{
        //    if (drawingCircle.drawingPoints.Count > 0)
        //    {

        //    }
        //}
        //private void circles_MouseMove(object sender, MouseEventArgs e)
        //{
        //    drawingCircle.mouseLastPoint = gMapControl1.FromLocalToLatLng(e.X, e.Y);
        //    gMapControl1.UpdateMarkerLocalPosition(drawingCircle);
        //    gMapControl1.Refresh();
        //}
        #endregion

        #region polygons
        //PointLatLng mouseLastPoint;
        private void polygons_MouseDown(object sender, MouseEventArgs e)
        {
            PointLatLng point = gMapControl1.FromLocalToLatLng(e.X, e.Y);
            if (drawingCircle.drawingPoints.Count == 0)
            {
                drawingCircle.isDrawed = false;
                drawingCircle.IsMeter = false;
                drawingCircle.Position = point;
                drawingCircle.drawingPoints.Add(point);
                gMapControl1.UpdateMarkerLocalPosition(drawingCircle);
                gMapControl1.Refresh();
            }
            else
            {
                //drawingCircle.drawingPoints.Add(point);
                drawingCircle.addPoint(point);
                switch (m_drawType)
                {
                    case AreaStruct.AreaDrawType.DT_CIRCLE:
                        if (drawingCircle.drawingPoints.Count >= 2)
                        {
                            drawingFinish(sender, e);
                        }
                        break;
                    case AreaStruct.AreaDrawType.DT_POLYGON:
                        if (drawingCircle.drawingPoints.Count <=1)
                        {
                            drawingCircle.drawingPoints.Add(point);
                        }
                        else if (drawingCircle.drawingPoints.Count > 1)
                        {
                            PointLatLng beginPointLat = drawingCircle.drawingPoints[0];
                            GPoint beginPoint= MercatorProjection.Instance.FromLatLngToPixel(beginPointLat.Lat, beginPointLat.Lat, (int)gMapControl1.Zoom);
                            GPoint  pointLoc = MercatorProjection.Instance.FromLatLngToPixel(point.Lat, point.Lat, (int)gMapControl1.Zoom);
                            int xDistance = (int)beginPoint.X - (int)pointLoc.X;
                            int yDistance = (int)beginPoint.Y - (int)pointLoc.Y;
                            double value = Math.Sqrt((double)(xDistance * xDistance + yDistance * yDistance));
                            if (value < 8.0)
                            {
                                drawingFinish(sender, e);
                                return;
                            }
                            else
                            {
                                drawingCircle.drawingPoints.Add(point);
                            }
                        }
                        //polygons_MouseMove(sender, e);
                        break;
                    case AreaStruct.AreaDrawType.DT_RECTANGLE:
                        if (drawingCircle.drawingPoints.Count >= 2)
                        {
                            drawingFinish(sender, e);
                        }
                        break;
                }
                gMapControl1.UpdateMarkerLocalPosition(drawingCircle);
                gMapControl1.Refresh();
            }


        }
        private void polygons_MouseMove(object sender, MouseEventArgs e)
        {
            drawingCircle.mouseLastPoint = gMapControl1.FromLocalToLatLng(e.X, e.Y);
            gMapControl1.UpdateMarkerLocalPosition(drawingCircle);
            gMapControl1.Refresh();
        }

        //private void polygons_MouseUp(object sender, MouseEventArgs e)
        //{
        //    //if (IsDrawingPolygon)
        //    //{
        //    //    drawingPolygon.Points.Clear();
        //    //    drawingPolygon.Points.Add(gMapControl1.FromLocalToLatLng(startX, startY));
        //    //    drawingPolygon.Points.Add(gMapControl1.FromLocalToLatLng(startX, e.Y));
        //    //    drawingPolygon.Points.Add(gMapControl1.FromLocalToLatLng(e.X, e.Y));
        //    //    drawingPolygon.Points.Add(gMapControl1.FromLocalToLatLng(e.X, startY));
        //    //    gMapControl1.UpdatePolygonLocalPosition(drawingPolygon);
        //    //    gMapControl1.Refresh();
        //    //}
        //}
        #endregion



        #region 按钮
        private void 跳转ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            gMapControl1.Position = MercatorProjection.Instance.FromPixelToLatLng(97756, 32340, 10);
            gMapControl1.Zoom = 9;
        }

        bool markersOverlayShow = false;
        private void btnShowTip_Click(object sender, EventArgs e)
        {
            markersOverlayShow = !markersOverlayShow;
            //markersOverlay.IsVisibile = markersOverlayShow;
        }


        private void btnMove_Click(object sender, EventArgs e)
        {
            drawingCircle.m_drawType = AreaStruct.AreaDrawType.DT_NONE;
            m_drawType = AreaStruct.AreaDrawType.DT_NONE;
            gMapControl1.DragButton = MouseButtons.Left;
        }
        #endregion
        public AreaStruct.AreaDrawType m_drawType;
        #region 图形按钮
        private void btnRect_Click(object sender, EventArgs e)
        {
            drawingCircle.m_drawType = AreaStruct.AreaDrawType.DT_RECTANGLE;
            m_drawType = AreaStruct.AreaDrawType.DT_RECTANGLE;
            gMapControl1.DragButton = MouseButtons.None;
        }

        private void btnCircle_Click(object sender, EventArgs e)
        {
            drawingCircle.m_drawType = AreaStruct.AreaDrawType.DT_CIRCLE;
            m_drawType = AreaStruct.AreaDrawType.DT_CIRCLE;
            gMapControl1.DragButton = MouseButtons.None;
        }

        private void btnPolygon_Click(object sender, EventArgs e)
        {
            drawingCircle.m_drawType = AreaStruct.AreaDrawType.DT_POLYGON;
            m_drawType = AreaStruct.AreaDrawType.DT_POLYGON;
            gMapControl1.DragButton = MouseButtons.None;
        }
        #endregion




    }
}
